package miiteen.twoexploit.controller;

import miiteen.common.json.JsonUtil;
import miiteen.common.utils.JacksonMapper;
import miiteen.core.result.ResultMessage;
import miiteen.core.result.ResultMessageTool;
import miiteen.data.beans.PageBounds;
import miiteen.hr.pubconfig.CurrentHrInfo;
import miiteen.hr.user.beans.UserUserInfo;
import miiteen.sys.base.controller.BaseAction;
import miiteen.sys.login.bean.UserShiro;
import miiteen.sys.login.utils.LoginUtil;
import miiteen.twoexploit.beans.Answering;
import miiteen.twoexploit.service.AnsweringService;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Name: lzhg_two_regulations
 * Des: -controller
 * Author: qyp
 * Date: 2021-10-20
 * 留言
 */
@Controller
@RequestMapping(value = "/two/answering")
public class AnsweringController extends BaseAction {

	@Autowired
	private AnsweringService answeringService;


	@RequiresPermissions("answering:list")
	@RequestMapping(value = "/list")
	public ModelAndView list() {
	    //设置列表关联JSP页面
		ModelAndView mv = new ModelAndView("twoexploit/answering/AnsweringList");
        //自行设置默认查询条件
        
		return mv;
	}
    
    @RequiresPermissions("answering:list")
    @RequestMapping("/search")
    @ResponseBody
    public String search(@RequestBody JSONObject jsonObject) {
        //获取查询条件
        PageBounds pageBounds = JsonUtil.jsonobjToObj(jsonObject.getJSONObject("pagecon"), PageBounds.class);
        Map<String, Object> map = JsonUtil.jsonobjToMap(jsonObject.getJSONObject("searchcon"));


        //获取登入用户角色
		UserShiro.ShiroUser shiroUser= LoginUtil.getCurUser();
		UserUserInfo user= CurrentHrInfo.getUserInfo(shiroUser);
		String deptName = user.getTrueName();
		map.put("deptName", deptName);

        //查询数据
        List<Answering> list = answeringService.findListByMap(map, pageBounds);
        if (list != null && list.size() > 0) {
            return JacksonMapper.getListToJson(ResultMessageTool.Success(String.valueOf(pageBounds.getTotal()), list));
        } else {
            return JacksonMapper.getListToJson(ResultMessageTool.Error(""));
        }
    }
      
	@RequiresPermissions("answering:edit")
	@RequestMapping(value = "/edit")
	public ModelAndView edit(@ModelAttribute("answering") Answering answering) {
		ModelAndView mv = new ModelAndView("twoexploit/answering/AnsweringEdit");

		if (answering.getId() > 0) {
			Answering bean = answeringService.findById(answering.getId());
			answering = bean;
		} 
        
        //返回对象JSON字符串
        mv.addObject("answeringdata", JsonUtil.toJsonStr(answering));
		return mv;
	}

	@RequiresPermissions("answering:edit")
	@RequestMapping(value = "/save")
	@ResponseBody
	public String save(@RequestBody Answering answering) {

		long result = answeringService.save(answering);
		if (result > 0) {
			return JsonUtil.toJsonStr(ResultMessageTool.SuccessRedirect("保存成功", "/two/answering/list"));
		} else {
		    return JsonUtil.toJsonStr(ResultMessageTool.Error("保存失败"));
		}
	}
    

	@RequiresPermissions("answering:del")
	@RequestMapping(value = "/del")
	@ResponseBody
	public String del(@RequestParam String ids) {
        ResultMessage resultMessage = new ResultMessage();
        if (!ids.equals("")) {
            int result = answeringService.delete(ids);
            if (result > 0) {
                resultMessage = ResultMessageTool.Success("删除成功");
            } else {
                resultMessage = ResultMessageTool.Error("删除失败");
            }
        } else {
            resultMessage = ResultMessageTool.Error("请选择要删除的数据");
        }
        return JsonUtil.toJsonStr(resultMessage);
	}
}